﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class User_E : PageBaseAdmin
    {

        #region 只读资料定义
        private readonly D_T_AdminUser_Exp _Dal = new D_T_AdminUser_Exp();
        private readonly D_T_PowerGroup_Exp _DalGroup = new D_T_PowerGroup_Exp();
        #endregion

        #region 固定方法
        /// <summary>
        ///  设置按钮资料
        /// </summary>
        /// <param name="i"></param>
        /// <param name="but"></param>
        private void SetBut(int i, Button but)
        {
            AdminPublic.Try(() =>
            {
                switch(i)
                {
                    case 1:
                        but.CausesValidation = false;
                        break;
                    default:
                        break;
                }
            });

        }
        /// <summary>
        /// 初始化窗口控件
        /// </summary>
        private void ControlsInit()
        {
            AdminPublic.Try(() =>
            {
                var LD = _DalGroup.GetListAll();
                this.DropDownList1.DataSource = LD;
                this.DropDownList1.DataTextField = "PowerGroupName";
                this.DropDownList1.DataValueField = "id";
                this.DropDownList1.DataBind();
            });
        }
        /// <summary>
        /// 编辑模式的时候,填充窗口
        /// </summary>
        /// <param name="model"></param>
        private void SetControlsData(M_T_AdminUser_Exp model)
        {
            AdminPublic.Try(() =>
            {
                this.DropDownList1.SelectedValue = model.PowerGroupID.Exp_Trim();
                this.TextBox2.Text = model.LoginName;
                this.TextBox1.Text = model.RealName;
                this.CheckBox1.Checked = model.IsAdmin;
            });
        }
        /// <summary>
        /// 从页面控件中构造一个实体model
        /// </summary>
        /// <returns></returns>
        private M_T_AdminUser_Exp ControlsToModel()
        {
            var model = new M_T_AdminUser_Exp()
            {
                PowerGroupID = this.DropDownList1.SelectedValue.Exp_IntTryParse(),
                Flag = true,
                id = ViewState["ID"].Exp_IntTryParse(),
                LoginName = TextBox2.Text.Exp_Trim(),
                PassWord = "abc".Exp_Encrypt(),
                RealName = TextBox1.Text.Exp_Trim(),
                IsAdmin = this.CheckBox1.Checked,
                PowerGroupName = ""
            };
            return model;
        }
        /// <summary>
        /// 操作完成后,处理各种提示用
        /// </summary>
        /// <param name="iRef"></param>
        /// <param name="LogType"></param>
        public void EditSaveMsg(int iRef, Log_Type LogType)
        {
            if(iRef > 0)
            {
                this.Exp_PageWriteLog(LogType, "用户资料维护", iRef);
                this.Exp_PageSkyAlterReplace("User.aspx", WebUserToolBar1.EditSKey, "操作成功");
            }
            else
            {
                switch(iRef)
                {
                    case 0:
                        this.Exp_PageAlter("操作失败,操作中出现异常");
                        break;
                    case -1:
                        this.Exp_PageAlter("操作失败,用户资料操作失败");
                        break;
                    case -2:
                        this.Exp_PageAlter("操作失败,权限组资料操作失败");
                        break;
                    default:
                        this.Exp_PageAlter("操作失败");
                        break;
                }
            }
        }
        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //工具栏初始化
                WebUserToolBar1.EditInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr);
                if(!IsPostBack)
                {
                    //初始化权限组下拉框
                    ControlsInit();
                    //存储主键
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("ID", id);
                    if(id > 0)
                    {
                        var model = _Dal.GetModelExp(id);
                        if(model == null)
                        {
                            this.Exp_PageSkyAlterReplace("User.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            SetControlsData(model);
                        }
                    }
                }

            });

        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //注意,统一,先获取实体,然后根据实体进行各种判断
                var model = ControlsToModel();

                var LogType = Log_Type.add;
                var PMode = _DalGroup.GetModelFlag(model.PowerGroupID);
                if(PMode == null)
                {
                    this.Exp_PageAlter("权限组资料不存在");
                    return;
                }
                if(_Dal.IsLogeName(model.LoginName, model.id))
                {
                    this.Exp_PageAlter("登录名不可以重复");
                    return;
                }

                var iRef = 0;
                if(0 >= model.id)
                {
                    iRef = _Dal.AddExp_1(model);
                }
                else
                {
                    //    修改的专用判断
                    LogType = Log_Type.update;
                    var Temp = _Dal.ExistsExp(model.id);
                    if(!Temp)
                    {
                        this.Exp_PageAlter("资料不存在,可能被其他人删除");
                        return;
                    }

                    iRef = _Dal.UpdateExp_1(model);
                }
                EditSaveMsg(iRef, LogType);
            });

        }

        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.EditSKey;
                Response.Redirect("User.aspx?" + sKey);
            });
        }
    }
}
